package com.sl.crawler.robot.parse.impl;

import com.sl.crawler.robot.parse.IParse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shiling on 2017/5/26.
 */
public class StringParse implements IParse<List<List<String>>, String> {
    private final static Logger LOG = LoggerFactory.getLogger(StringParse.class);

    public List<Map<String, String>> parse(List<List<String>> xpathConfig, String pageSource) {
        List<Map<String, String>> resultList = new ArrayList<>();
        Document document = Jsoup.parse(pageSource);

        // 获取循环元素个数
        Elements htmlElementList = document.select(xpathConfig.get(0).get(2));
        for (Element element : htmlElementList) {
            Map<String, String> resultMap = new LinkedHashMap<>();
            for (List<String> config : xpathConfig) {
                String xpath = config.get(3);
                String fieldName = config.get(0);
                Elements fieldElements = element.select(xpath);
                if (CollectionUtils.isEmpty(fieldElements)) {
                    continue;
                }
                Element domNode = fieldElements.get(0);
                String fieldValue = "";
                String attribute = "";
                if (config.size() > 4) {
                    attribute = config.get(4);
                }
                if (StringUtils.equalsIgnoreCase("ownText", attribute)) {
                    fieldValue = domNode.ownText();
                } else if (StringUtils.isNotBlank(attribute)) {
                    fieldValue = domNode.attr(attribute);
                } else {
                    fieldValue = domNode.text();
                }
                resultMap.put(fieldName, fieldValue);
            }
            resultList.add(resultMap);
        }
        return resultList;
    }

}
